<?php

class ErrorType
{
	const NotFound = "NotFound";
	const InsufficientPrivilege = "InsufficientPrivilege";
	const NotAuthenticated = "NotAuthenticated";
	const Kernel = "Kernel";
	const Generality = "Generality";
	const DataBase = "DataBase";
	const Engine = "HtmlStudio Engine";
	const Processor = "Processor Paramiter";
	const Util = "HtmlStudio Util Tools";
	const Object = "HtmlStudio Objects Collections";
	const Module = "HtmlStudio Module Erros";
}
/**
*This is base engine exception
*@package com.HtmlStudio
*@subpackage HtmlStudio.kernel
*/
class HtmlStudioException extends Exception 
{
	/**
	 * Error Type
	 *
	 * @var ErrorType
	 */
	public $errorType = ErrorType::Kernel;
	/**
	 * Exception Class name
	 *
	 * @var string
	 */
	public $erroClass;
	/**
	 * Module occurred erro
	 *
	 * @var string
	 */
	public $moduleName;
	/**
	 * Show stack trace?
	 *
	 * @var bool
	 */
	public $showStackTrace = true;
	/**
	 * HtmlStudioException constructor
	 *
	 * @return HtmlStudioException
	 * @param int $code
	 * @param string $message
	 */
	function HtmlStudioException($code = 0, $message = "")
	{
		$this->code = $code;
		$this->message = $message;
		$this->setExceptionClassName($this);
	}
	/**
	 * Config exception type
	 *
	 * @param ErrorType $type
	 */
	public function setErrorType($type)
	{
		$this->errorType = $type;
	}
	/**
	 * Config Exception class name
	 *
	 * @param Exception $ex
	 */
	public function setExceptionClassName($ex)
	{
		$this->erroClass = get_class($ex);
	}
	/**
	 * Config range code error
	 *
	 * @param int $start
	 * @param int $end
	 */
	public function setRangeCode($start, $end)
	{
		if ($this->code < $start) {
			$this->code += $start;
		}
		if ($this->code > $end) {
			while (($this->code = $this->code / 10) > $end) {}
			$this->code = round($this->code) + $start;
		}
	}
}
/**
*This is Not Found exception type
*@package com.HtmlStudio
*@subpackage HtmlStudio.kernel
*/
class NotFoundException extends HtmlStudioException 
{
	/**
	 * NotFoundException constructor
	 *
	 * @return NotFoundException
	 * @param string $message
	 */
	function NotFoundException($message = "")
	{
		parent::HtmlStudioException(404, $message);
		$this->errorType = ErrorType::NotFound;
		$this->showStackTrace = false;
	}
}

Class ExceptionModule extends HtmlStudioException
{
	
}


/**
*This File Util exception type
* Range code error: 100 to 249
*@package com.HtmlStudio
*@subpackage HtmlStudio.kernel
*/
class FileUtilException extends HtmlStudioException 
{
	/**
	 * FileUtilException constructor
	 * Range code error: 100 to 299
	 *
	 * @return FileUtilException
	 * @param int $code
	 * @param string $message
	 */
	function FileUtilException($code, $message = "")
	{
		parent::HtmlStudioException($code, $message);
		$this->errorType = ErrorType::Util;
		$this->setRangeCode(100, 249);
	}
}

/**
*This is database exception type
* Range code error: 1000 to 2000
*@package com.HtmlStudio
*@subpackage HtmlStudio.kernel
*/
class DataBaseException extends HtmlStudioException 
{
	public $dbms;
	public $fn;
	public $sql = '';
	public $params = '';
	public $host = '';
	public $database = '';
	/**
	 * DataBaseException constructor
	 *
	 * @return DataBaseException
	 * @param int $code
	 * @param string $message
	 */
	function DataBaseException($dbms, $fn, $errno = '', $errmsg = '', $p1 = '', $p2 = '', $thisConnection = '')
	{
		parent::HtmlStudioException($errno, $errmsg);
		$this->errorType = ErrorType::DataBase;
		switch($fn) {
		case 'EXECUTE':
			$this->sql = $p1;
			$this->params = $p2;
			$s = "$dbms error: [$errno: $errmsg] in $fn(\"$p1\")\n";
			break;
	
		case 'PCONNECT':
		case 'CONNECT':
			$user = $thisConnection->user;
			$s = "$dbms error: [$errno: $errmsg] in $fn($p1, '$user', '****', $p2)\n";
			break;
		default:
			$s = "$dbms error: [$errno: $errmsg] in $fn($p1, $p2)\n";
			break;
		}
	
		$this->dbms = $dbms;
		$this->host = $thisConnection->host;
		$this->database = $thisConnection->database;
		$this->fn = $fn;
		$this->msg = $errmsg;
	}
}
/**
* This is Module exception type
* Range code error: 5000 to 7000
*@package com.HtmlStudio
*@subpackage HtmlStudio.kernel
*/
class ModuleExecption extends HtmlStudioException 
{
	/**
	 * ModuleExecption constructor
	 *
	 * @return ModuleExecption
	 * @param int $code
	 * @param string $message
	 */
	function ModuleExecption($message = "", $code = 0)
	{
		parent::HtmlStudioException($code, $message);
		$this->errorType = ErrorType::Module;
		$this->setRangeCode(5000, 7000);
	}
}
?>